// 获取歌曲 src
const getAllSongSrc = () => {
    let songList = es('.list-song')
    // log('songList', songList)
    let arr = []
    for (let i = 0; i < songList.length; i++) {
        let a = songList[i].dataset.path
        // log('thesong: ', a, typeof a)
        arr.push(a)
    }
    // log('获取歌曲 src: ', arr)
    return arr
}

// 获取所有歌曲名称
const getAllSongName = () => {
    let songList = es('.list-song')
    // log('songList', songList)
    let arr = []
    for (let i = 0; i < songList.length; i++) {
        let a = songList[i].innerHTML
        arr.push(a)
    }
    // log('获取所有歌曲名称', arr)
    return arr
}

const getSongName = (audio) => {
    let index = getPlay(audio)
    let allSongName = getAllSongName()
    let songName = allSongName[index]
    return songName
}

// 暂停/播放
const bindEventPlayAndPause = (audio) => {
    getPlay(audio)

    let button = e('#id-btn-play')
    let pap = e('#id-play-pause')
    button.addEventListener('click', () => {
        if (button.classList.contains('status')) {
            if (pap.classList.contains('fa-play')) {
                log('播放')

                audio.play()
                button.classList.remove('status')
                pap.classList.remove('fa-play')
                pap.classList.add('fa-pause')
            }
        } else {
            if (pap.classList.contains('fa-pause')) {
                log('暂停')
                audio.pause()
                button.classList.add('status')

                pap.classList.remove('fa-pause')
                pap.classList.add('fa-play')
            }
        }
    })
}

//自动播放下一首
const bindEventEnded = (audio) => {
    audio.addEventListener('ended', function () {
        if (audio.classList.contains("list")) {
            let song = 'audio/' + nextPlay(audio)
            log('列表播放', song)
            audio.src = song
            audio.play()
        } else if (audio.classList.contains("loop")) {
            audio.currentTime = 0
            audio.play()
            log('单曲循环')
        } else if (audio.classList.contains("random")) {
            let song = 'audio/' + randomSong()
            log('随机播放', song)
            audio.src = song
            audio.play()
        } else {
            audio.classList.add("list")
            let song = 'audio/' + nextPlay(audio)
            log('列表播放', song)
            audio.src = song
            audio.play()
        }
    })
}

//获取当前正在播放的音乐的索引值
const getPlay = (audio) => {
    let arr = getAllSongSrc()
    let src = audio.src.split('/').slice(-1)[0]
    // log('当前歌曲 src: ', src)
    let index = arr.indexOf(src)
    // log('idnex 第几首歌曲', index)
    return index
}

// 下一首
const nextPlay = (audio) => {
    let arr = getAllSongSrc()
    let index = getPlay(audio)
    let next = (index + 1 + arr.length) % arr.length
    // log('Next song', next, arr[next])
    return arr[next]
}

const bindEventNext = (audio) => {
    let nextSong = e("#id-btn-next")
    let button = e('#id-btn-play')
    let idPlay = e('#id-play-pause')
    nextSong.addEventListener('click', () => {
        if (button.classList.contains('status')) {
            button.classList.remove('status')
            idPlay.classList.remove('fa-play')
            idPlay.classList.add('fa-pause')
        }
        log('下一首')
        let song = ' audio/' + nextPlay(audio)
        audio.src = song
        audio.play()
    })
}

// 上一首
const previousPlay = (audio) => {
    let arr = getAllSongSrc()
    let index = getPlay(audio)
    let previous = (index - 1 + arr.length) % arr.length
    // log('previous', previous)
    return arr[previous]
}

const bindEventPrevious = (audio) => {
    let previousSong = e("#id-btn-previous")
    let button = e('#id-btn-play')
    let idPlay = e('#id-play-pause')
    previousSong.addEventListener('click', () => {
        log('上一首')
        if (button.classList.contains('status')) {
            button.classList.remove('status')
            idPlay.classList.remove('fa-play')
            idPlay.classList.add('fa-pause')
        }
        let song = ' audio/' + previousPlay(audio)
        audio.src = song
        audio.play()
    })
}

const choice = (arr) => {
    // 产生一个随机数
    let a = Math.random() * (arr.length)
    let index = Math.floor(a)
    return arr[index]
}

// 随机播放
const randomSong = () => {
    let songs = getAllSongSrc()
    let s = choice(songs)
    return s
}

const bindEventRandom = (audio) => {
    let random = e('#id-footer-random')
    random.addEventListener('click', function () {
        log('随机播放')
        audio.classList.remove('list')
        audio.classList.remove('loop')
        audio.classList.add('random')
    })
}

// 单曲循环
const bindEventSingle = (audio) => {
    let singleId = e("#id-footer-single")
    singleId.addEventListener('click', () => {
        // 添加一个 loop
        log('单曲循环')
        audio.classList.remove('list')
        audio.classList.remove('random')
        audio.classList.add('loop')
    })
}

const bindEvents = () => {
    let audio = e('#id-audio-player')
    setInterval(() => {
        let hh2 = e('#id-song-name')
        hh2.innerHTML = getSongName(audio)
    }, 500)

    bindEventEnded(audio)
    bindEventPlayAndPause(audio)
    bindEventNext(audio)
    bindEventPrevious(audio)
    bindEventSingle(audio)
    bindEventRandom(audio)
    getAllSongSrc()
    getAllSongName()
}

const __main = () => {
    bindEvents()
}

__main()